package com.cocotingo.snail.template.models;

import com.cocotingo.snail.template.TemplateSyntaxException;

public abstract class PaintFactorNode extends Node {

	public static final int IMAGE = 0;
	
	public abstract int getType();
	
	@Override
	public Iterable<Node> children() {
		return Node.EMPTY_ITERABLE;
	}
	
	public static class PaintImageNode extends PaintFactorNode {

		public static final int STRETCH = 0;
		public static final int TILE = 1;
		public static final int CENTER = 2;
		
		private int style;
		private String image;
		
		public PaintImageNode() {
			style = STRETCH;
		}
		
		public int getStyle() {
			return style;
		}
		
		public void setStyle(int style) {
			this.style = style;
		}
		
		public String getImageID() {
			return image;
		}
		
		public void setImageID(String id) {
			this.image = id;
		}
		
		@Override
		public int getType() {
			return IMAGE;
		}

		@Override
		public void validateNode() throws TemplateSyntaxException {
			if (style < 0 || style > 2)
				throw new TemplateSyntaxException(this, "Illegal ImageNode.style: " + style + ".");
			if (image == null || image.isEmpty()) throw new TemplateSyntaxException(this, "ImageNode.resourceID shouldn't be empty/null.");
		}

		@Override
		protected String getXMLNodeName() {
			return "PaintImage";
		}

		@Override
		protected String getXMLAttributes() {
			StringBuffer buf = new StringBuffer(String.format("image=\"%s\"", image));
			switch (style) {
			case STRETCH:
				buf.append(" style=\"stretch\"");
				break;
			case TILE:
				buf.append(" style=\"tile\"");
				break;
			case CENTER:
				buf.append(" style=\"center\"");
				break;
			}
			return buf.toString();
		}
		
	}
	
}
